﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace BLL.Concrete
{
    public class HomeGaleriaBll : BllConnection
    {
        public IQueryable<DataAccess.HomeGaleria> GetGaleria()
        {
            return db.HomeGalerias;
        }

        public int InsertImage(int? orden, HttpPostedFileBase imagen)
        {
            ImagenBll imageBll = new ImagenBll();
            int idImagen = imageBll.InsertImage(imagen);

            int ord = orden == null ? 1 : orden.Value;

            DataAccess.HomeGaleria img = new DataAccess.HomeGaleria
            {
                Orden = ord,
                IdImagen = idImagen
            };
            db.HomeGalerias.InsertOnSubmit(img);
            db.SubmitChanges();
            return(img.Id);
        }

        public void updateOrden(int idImagenGaleria, int? orden)
        {
            var img = GetGaleria().Single(u => u.Id == idImagenGaleria);
            int ord = orden == null ? 1 : orden.Value;

            img.Orden = ord;
            db.SubmitChanges();            
        }

        public void DeleteImagenGaleria(int idImagenGaleria)
        {
            ImagenBll imageBll = new ImagenBll();
            var img = GetGaleria().Single(u => u.Id == idImagenGaleria);
            int idImg = img.IdImagen;
            db.HomeGalerias.DeleteOnSubmit(img);
            db.SubmitChanges();

            imageBll.EliminarImagen(idImg);
        }
    }
}
